import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs
import Qt.labs.folderlistmodel
import QtQuick.Controls.Material
import QtCore

Item {
    id: root

    FolderListModel {
        id: folderModel
        folder: StandardPaths.standardLocations(StandardPaths.HomeLocation)[0]
        showDirsFirst: true
        nameFilters: ["*"]
    }
    
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 15
        spacing: 15
        
        Pane {
            Layout.fillWidth: true
            Material.elevation: 1
            
            RowLayout {
                anchors.fill: parent
                spacing: 12
                
                Button {
                    text: "选择文件夹"
                    icon.source: "/images/icons/folder.svg"
                    Material.background: Material.primary
                    Material.foreground: "white"
                    onClicked: folderDialog.open()
                }
                
                Label {
                    text: folderModel.folder.toString().replace("file://", "")
                    elide: Text.ElideLeft
                    Layout.fillWidth: true
                    font.pixelSize: 14
                    color: Material.foreground
                }
            }
        }
        
        Pane {
            Layout.fillWidth: true
            Layout.fillHeight: true
            Material.elevation: 1
            padding: 0
            
            ColumnLayout {
                anchors.fill: parent
                spacing: 0
                
                Rectangle {
                    Layout.fillWidth: true
                    height: 40
                    color: Material.backgroundColor
                    z: 2
                    
                    RowLayout {
                        anchors.fill: parent
                        anchors.leftMargin: 16
                        anchors.rightMargin: 16
                        spacing: 8
                        
                        Label {
                            text: ""
                            Layout.preferredWidth: 32
                            font.bold: true
                        }
                        Label {
                            text: "文件名"
                            Layout.fillWidth: true
                            font.bold: true
                        }
                        Label {
                            text: "类型"
                            Layout.preferredWidth: 100
                            font.bold: true
                        }
                        Label {
                            text: "大小"
                            Layout.preferredWidth: 100
                            font.bold: true
                        }
                        Label {
                            text: "修改日期"
                            Layout.preferredWidth: 150
                            font.bold: true
                        }
                    }
                    
                    Rectangle {
                        anchors.bottom: parent.bottom
                        width: parent.width
                        height: 1
                        color: Material.dividerColor
                    }
                }
                
                ListView {
                    id: listView
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    model: folderModel
                    clip: true
                    
                    ScrollBar.vertical: ScrollBar {
                        active: true
                    }
                    
                    delegate: ItemDelegate {
                        id: delegate
                        width: listView.width
                        height: 48
                        
                        required property string fileName
                        required property bool fileIsDir
                        required property url filePath
                        required property int fileSize
                        required property date fileModified
                        
                        background: Rectangle {
                            color: delegate.hovered ? Material.listHighlightColor : "transparent"
                        }
                        
                        RowLayout {
                            anchors.fill: parent
                            anchors.leftMargin: 16
                            anchors.rightMargin: 16
                            spacing: 8
                            
                            Image {
                                source: fileIsDir ? "qrc:/images/icons/folder.svg" : "qrc:/images/icons/file.svg"
                                Layout.preferredWidth: 24
                                Layout.preferredHeight: 24
                            }
                            
                            Label {
                                text: fileName
                                elide: Text.ElideRight
                                Layout.fillWidth: true
                                color: fileIsDir ? Material.accent : Material.foreground
                            }
                            
                            Label {
                                text: fileIsDir ? "文件夹" : fileName.split('.').pop()
                                Layout.preferredWidth: 100
                                color: Material.foreground
                            }
                            
                            Label {
                                text: fileIsDir ? "--" : (fileSize < 1024 ? fileSize + " B" : 
                                      fileSize < 1048576 ? Math.round(fileSize/1024) + " KB" : 
                                      Math.round(fileSize/1048576) + " MB")
                                Layout.preferredWidth: 100
                                color: Material.foreground
                            }
                            
                            Label {
                                text: Qt.formatDateTime(fileModified, "yyyy-MM-dd hh:mm")
                                Layout.preferredWidth: 150
                                color: Material.foreground
                            }
                        }
                        
                        Rectangle {
                            anchors.bottom: parent.bottom
                            width: parent.width
                            height: 1
                            color: Material.dividerColor
                        }
                    }
                }
            }
        }
    }
    
    FolderDialog {
        id: folderDialog
        title: "选择文件夹"
        currentFolder: folderModel.folder
        onAccepted: folderModel.folder = selectedFolder
    }
} 
